class code1{
    public boolean isValid(String s) {
        Map<Character,Character> map = new HashMap<>();
        map.put('}','{');
        map.put(']','[');
        map.put(')','(');
        StringBuilder st = new StringBuilder();
        char[] ss = s.toCharArray();

        for(char ch : ss) {
            if (map.containsValue(ch)) {
                //左括号
                st.append(ch);
            } else {
                //右括号
                if (!st.isEmpty() && st.charAt(st.length()-1) == map.get(ch)) {
                    st.deleteCharAt(st.length()-1);
                } else {
                    return false;
                }
            }
        }
        //注意看栈是否消费完
        return st.isEmpty();
    }
}